8/26/2019

Mapas dinâmicos ou bonitinhos

  • Base pública de dados vetoriais / raster (rnaturalearth)
  • Trazer mapas de fundo de serviços online (ggmap)
  • Mapas dinâmicos (leaflet, mapview)

Pacote rnaturalearth

https://www.naturalearthdata.com/

Base de dados de domínio público, nas escalas 1:10m, 1:50m, and 1:110 milhões. Contém dados no formato vetorial e raster para uso em cartografia ou GIS.

  • rnaturalearth: funções para acessar os dados
  • rnaturalearthdata: dados vetoriais
  • rnaturalearthhires: dados vetoriais com alta resolução

Lista de dados disponíveis https://www.naturalearthdata.com/features/

Pacote rnaturalearth

library(rnaturalearth)
library(rnaturalearthdata)

ufs <- ne_states(country = 'Brazil', returnclass = 'sf')
ggplot(ufs) + geom_sf()

rnaturalearth

world <- ne_countries(scale = "medium", returnclass = "sf")
ggplot(data = world) +
    geom_sf(aes(fill = pop_est)) +
    scale_fill_viridis_c(option = "plasma", trans = "sqrt")

ggmap

ggmap

library(ggmap)
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
perfis <- readRDS('data/perfis.RDS')
br <- c(left = -75, bottom = -35, right = -33, top = 6)
brasil <- get_map(br, source = "stamen")
## Source : http://tile.stamen.com/terrain/5/9/15.png
## Source : http://tile.stamen.com/terrain/5/10/15.png
## Source : http://tile.stamen.com/terrain/5/11/15.png
## Source : http://tile.stamen.com/terrain/5/12/15.png
## Source : http://tile.stamen.com/terrain/5/13/15.png
## Source : http://tile.stamen.com/terrain/5/9/16.png
## Source : http://tile.stamen.com/terrain/5/10/16.png
## Source : http://tile.stamen.com/terrain/5/11/16.png
## Source : http://tile.stamen.com/terrain/5/12/16.png
## Source : http://tile.stamen.com/terrain/5/13/16.png
## Source : http://tile.stamen.com/terrain/5/9/17.png
## Source : http://tile.stamen.com/terrain/5/10/17.png
## Source : http://tile.stamen.com/terrain/5/11/17.png
## Source : http://tile.stamen.com/terrain/5/12/17.png
## Source : http://tile.stamen.com/terrain/5/13/17.png
## Source : http://tile.stamen.com/terrain/5/9/18.png
## Source : http://tile.stamen.com/terrain/5/10/18.png
## Source : http://tile.stamen.com/terrain/5/11/18.png
## Source : http://tile.stamen.com/terrain/5/12/18.png
## Source : http://tile.stamen.com/terrain/5/13/18.png
## Source : http://tile.stamen.com/terrain/5/9/19.png
## Source : http://tile.stamen.com/terrain/5/10/19.png
## Source : http://tile.stamen.com/terrain/5/11/19.png
## Source : http://tile.stamen.com/terrain/5/12/19.png
## Source : http://tile.stamen.com/terrain/5/13/19.png

ggmap

ggmap(brasil) +
  geom_point(data = perfis, aes(x = lon, y = lat)) +
  labs(title = "Soil profiles in Hybras database")
## Warning: Removed 4 rows containing missing values (geom_point).

ggmap

sul <- c(left = -60, bottom = -35, right = -33, top = -20)
##br_sul <- get_map(sul, maptype = "toner-lite", source = 'stamen', force = TRUE) ## bug - trazendo mapa errado
br_sul <- get_stamenmap(sul, maptype = "toner-lite", zoom = 6, force = TRUE)
## Source : http://tile.stamen.com/toner-lite/6/21/35.png
## Source : http://tile.stamen.com/toner-lite/6/22/35.png
## Source : http://tile.stamen.com/toner-lite/6/23/35.png
## Source : http://tile.stamen.com/toner-lite/6/24/35.png
## Source : http://tile.stamen.com/toner-lite/6/25/35.png
## Source : http://tile.stamen.com/toner-lite/6/26/35.png
## Source : http://tile.stamen.com/toner-lite/6/21/36.png
## Source : http://tile.stamen.com/toner-lite/6/22/36.png
## Source : http://tile.stamen.com/toner-lite/6/23/36.png
## Source : http://tile.stamen.com/toner-lite/6/24/36.png
## Source : http://tile.stamen.com/toner-lite/6/25/36.png
## Source : http://tile.stamen.com/toner-lite/6/26/36.png
## Source : http://tile.stamen.com/toner-lite/6/21/37.png
## Source : http://tile.stamen.com/toner-lite/6/22/37.png
## Source : http://tile.stamen.com/toner-lite/6/23/37.png
## Source : http://tile.stamen.com/toner-lite/6/24/37.png
## Source : http://tile.stamen.com/toner-lite/6/25/37.png
## Source : http://tile.stamen.com/toner-lite/6/26/37.png
## Source : http://tile.stamen.com/toner-lite/6/21/38.png
## Source : http://tile.stamen.com/toner-lite/6/22/38.png
## Source : http://tile.stamen.com/toner-lite/6/23/38.png
## Source : http://tile.stamen.com/toner-lite/6/24/38.png
## Source : http://tile.stamen.com/toner-lite/6/25/38.png
## Source : http://tile.stamen.com/toner-lite/6/26/38.png

ggmap

ggmap(br_sul) +
  geom_point(data = perfis, aes(x = lon, y = lat, col = BR_Soil_Taxonomy)) +
  labs(title = "Soil profiles in Hybras database")
## Warning: Removed 116 rows containing missing values (geom_point).

ggmap

cps_bb <- c(left = -47.1, bottom = -22.9, right = -47, top = -22.8)
cps <- get_stamenmap(cps_bb, zoom = 14, maptype = "toner-lite")
## Source : http://tile.stamen.com/toner-lite/14/6048/9258.png
## Source : http://tile.stamen.com/toner-lite/14/6049/9258.png
## Source : http://tile.stamen.com/toner-lite/14/6050/9258.png
## Source : http://tile.stamen.com/toner-lite/14/6051/9258.png
## Source : http://tile.stamen.com/toner-lite/14/6052/9258.png
## Source : http://tile.stamen.com/toner-lite/14/6048/9259.png
## Source : http://tile.stamen.com/toner-lite/14/6049/9259.png
## Source : http://tile.stamen.com/toner-lite/14/6050/9259.png
## Source : http://tile.stamen.com/toner-lite/14/6051/9259.png
## Source : http://tile.stamen.com/toner-lite/14/6052/9259.png
## Source : http://tile.stamen.com/toner-lite/14/6048/9260.png
## Source : http://tile.stamen.com/toner-lite/14/6049/9260.png
## Source : http://tile.stamen.com/toner-lite/14/6050/9260.png
## Source : http://tile.stamen.com/toner-lite/14/6051/9260.png
## Source : http://tile.stamen.com/toner-lite/14/6052/9260.png
## Source : http://tile.stamen.com/toner-lite/14/6048/9261.png
## Source : http://tile.stamen.com/toner-lite/14/6049/9261.png
## Source : http://tile.stamen.com/toner-lite/14/6050/9261.png
## Source : http://tile.stamen.com/toner-lite/14/6051/9261.png
## Source : http://tile.stamen.com/toner-lite/14/6052/9261.png
## Source : http://tile.stamen.com/toner-lite/14/6048/9262.png
## Source : http://tile.stamen.com/toner-lite/14/6049/9262.png
## Source : http://tile.stamen.com/toner-lite/14/6050/9262.png
## Source : http://tile.stamen.com/toner-lite/14/6051/9262.png
## Source : http://tile.stamen.com/toner-lite/14/6052/9262.png
## Source : http://tile.stamen.com/toner-lite/14/6048/9263.png
## Source : http://tile.stamen.com/toner-lite/14/6049/9263.png
## Source : http://tile.stamen.com/toner-lite/14/6050/9263.png
## Source : http://tile.stamen.com/toner-lite/14/6051/9263.png
## Source : http://tile.stamen.com/toner-lite/14/6052/9263.png

ggmap

ggmap(cps) +
  geom_point(data = perfis, aes(x = lon, y = lat, col = BR_Soil_Taxonomy)) +
  labs(title = "Soil profiles in Hybras database")
## Warning: Removed 515 rows containing missing values (geom_point).

Leaflet

  • Biblioteca JavaScript para criar mapas interativos
  • Mobile friendly
  • Pacote R para gerar os mapas mais facilmente
  • leafem: extensão que permite adicionar camadas sf e outras coisinhas
  • mapview: baseia-se no leaflet + leafem para gerar mapas de forma rápida

Leaflet

require(leaflet)
## Loading required package: leaflet
m <- leaflet()
m <- addTiles(m)
m <- setView(m, -50, -20, zoom=4)

m <- addMarkers(m, lng=-47, lat=-23, popup="Campinas") # Oncoto?

# adicionando do geoserver do IBGE
m <- addWMSTiles(m, 'https://geoservicos.ibge.gov.br/geoserver/ows',
                 layers='CCAR:BC250_Terra_Indigena_A',
                 options = WMSTileOptions(format = "image/png",
                                          transparent=TRUE),
                 attribution = 'teste')

Leaflet

Leaflet 2

Preparando os dados

library(mapview)
library(leafem)

koppen_orig <- read_stars('data/koppen_zarc_resample.tif')
koppen <- st_warp(koppen_orig, crs = st_crs(3857), method = "ngb")
#koppen <- projectRasterForLeaflet(koppen_orig, method = "ngb")

cores <- c("#7bae65", "#314999", "#221f65", "#aac31b", "#549a48", "#b8c016",
                     "#e8e607", "#45922a", "#d39525", "#8ec0e1", "#275c26", "#3a7bc7")
legenda <- c("Cwa", "Am", "Af", "Cfa", "Cwb", "Csb", "Csa", "Cfb",
                       "BSh", "As", "Cwc", "Aw")

pal <- colorFactor(cores, 1:12, na.color = 'transparent')

Warnings e mensagens suprimidas

Leaflet 2

Gerando o mapa

m <- leaflet() %>%
  addTiles(group = 'OSM') %>%
  # addRasterImage(koppen, group = 'koppen', project = FALSE, colors = pal) %>%
  addStarsImage(koppen, group = 'koppen', project = FALSE, colors = pal) %>%
  leafem::addFeatures(ufs, color = 'black', weight = 2, opacity = 1,
                      fill = FALSE, group = 'estado' ) %>%
  addMarkers(lng = perfis$lon, lat = perfis$lat, popup = perfis$BR_Soil_Taxonomy) %>%
  addLayersControl(
    overlayGroups = c('koppen', 'estado')) %>%
  addLegend("bottomleft", 
            colors = cores,
            labels = legenda,
            opacity = 1
            )

Warnings e mensagens suprimidas

Leaflet 2

Exemplos no RPubs

Referências

Fim da parte com mapas dinâmicos

sessionInfo

## R version 3.4.4 (2018-03-15)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 16.04.6 LTS
## 
## Matrix products: default
## BLAS: /usr/lib/libblas/libblas.so.3.6.0
## LAPACK: /usr/lib/lapack/liblapack.so.3.6.0
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] leafem_0.0.1            mapview_2.7.0          
##  [3] leaflet_2.0.2           ggmap_3.0.0            
##  [5] rnaturalearthdata_0.1.0 rnaturalearth_0.1.0    
##  [7] stars_0.3-2             abind_1.4-5            
##  [9] sf_0.7-7                forcats_0.4.0          
## [11] stringr_1.4.0           dplyr_0.8.3            
## [13] purrr_0.3.2             readr_1.3.1            
## [15] tidyr_0.8.3             tibble_2.1.3           
## [17] ggplot2_3.2.0           tidyverse_1.2.1        
## 
## loaded via a namespace (and not attached):
##  [1] nlme_3.1-137             bitops_1.0-6            
##  [3] satellite_1.0.1          lubridate_1.7.4         
##  [5] webshot_0.5.1            httr_1.4.0              
##  [7] tools_3.4.4              backports_1.1.4         
##  [9] R6_2.4.0                 KernSmooth_2.23-15      
## [11] rgeos_0.4-3              DBI_1.0.0               
## [13] lazyeval_0.2.2           colorspace_1.4-1        
## [15] raster_2.9-23            withr_2.1.2             
## [17] sp_1.3-1                 tidyselect_0.2.5        
## [19] curl_4.0                 compiler_3.4.4          
## [21] cli_1.1.0                rvest_0.3.4             
## [23] xml2_1.2.1               labeling_0.3            
## [25] scales_1.0.0             classInt_0.3-3          
## [27] digest_0.6.20            rmarkdown_1.14          
## [29] base64enc_0.1-3          jpeg_0.1-8              
## [31] pkgconfig_2.0.2          htmltools_0.3.6         
## [33] htmlwidgets_1.3          rlang_0.4.0             
## [35] readxl_1.3.1             rstudioapi_0.10         
## [37] shiny_1.3.2              generics_0.0.2          
## [39] jsonlite_1.6             crosstalk_1.0.0         
## [41] magrittr_1.5             Rcpp_1.0.2              
## [43] munsell_0.5.0            stringi_1.4.3           
## [45] yaml_2.2.0               plyr_1.8.4              
## [47] grid_3.4.4               parallel_3.4.4          
## [49] promises_1.0.1           crayon_1.3.4            
## [51] lattice_0.20-38          haven_2.1.1             
## [53] hms_0.5.0                zeallot_0.1.0           
## [55] knitr_1.23               pillar_1.4.2            
## [57] rjson_0.2.20             codetools_0.2-15        
## [59] stats4_3.4.4             glue_1.3.1              
## [61] evaluate_0.14            modelr_0.1.4            
## [63] png_0.1-7                vctrs_0.2.0             
## [65] httpuv_1.5.1             RgoogleMaps_1.4.3       
## [67] cellranger_1.1.0         gtable_0.3.0            
## [69] assertthat_0.2.1         xfun_0.8                
## [71] mime_0.7                 lwgeom_0.1-7            
## [73] xtable_1.8-4             broom_0.5.2             
## [75] e1071_1.7-2              later_0.8.0             
## [77] rnaturalearthhires_0.2.0 class_7.3-14            
## [79] viridisLite_0.3.0        units_0.6-3